import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * profileOrg.js
 * @date 2018/10/11

 */
import { createPagination, getResponse } from 'utils/utils';
import { queryMapIdpValue } from 'cpaas-front/lib/services/api';
import { profileOrgQueryPage, profileOrgRemoveOne, profileOrgQueryProfileValue, profileValueOrgRemoveOne, profileOrgSaveOne // profileSiteQueryProfileValue,
} from "../services/profileService";
export default {
  namespace: 'profileOrg',
  state: {
    enumMap: {},
    // 存储 当前页 所有的 配置项
    list: [],
    pagination: {},
    // 编辑框 是否显示
    editModalVisible: false,
    // 配置维护详情
    profileValue: {}
  },
  effects: {
    // 获取值集 并存入 enumMap
    fetchBatchEnums: /*#__PURE__*/_regeneratorRuntime.mark(function fetchBatchEnums(_, _ref) {
      var call, put, res;
      return _regeneratorRuntime.wrap(function fetchBatchEnums$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.t0 = getResponse;
              _context.next = 4;
              return call(queryMapIdpValue, {
                // 应用维度
                levelCode: 'CPFM.PROFILE.LEVEL_CODE'
              });

            case 4:
              _context.t1 = _context.sent;
              res = (0, _context.t0)(_context.t1);

              if (!res) {
                _context.next = 9;
                break;
              }

              _context.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  enumMap: res
                }
              });

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, fetchBatchEnums);
    }),
    // 获取配置数据
    profileFetchList: /*#__PURE__*/_regeneratorRuntime.mark(function profileFetchList(_ref2, _ref3) {
      var _ref2$payload, organizationId, payload, call, put, res;

      return _regeneratorRuntime.wrap(function profileFetchList$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _ref2$payload = _ref2.payload, organizationId = _ref2$payload.organizationId, payload = _ref2$payload.payload;
              call = _ref3.call, put = _ref3.put;
              _context2.t0 = getResponse;
              _context2.next = 5;
              return call(profileOrgQueryPage, organizationId, payload);

            case 5:
              _context2.t1 = _context2.sent;
              res = (0, _context2.t0)(_context2.t1);

              if (!res) {
                _context2.next = 10;
                break;
              }

              _context2.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  list: res.content,
                  pagination: createPagination(res)
                }
              });

            case 10:
            case "end":
              return _context2.stop();
          }
        }
      }, profileFetchList);
    }),
    profileRemoveOne: /*#__PURE__*/_regeneratorRuntime.mark(function profileRemoveOne(_ref4, _ref5) {
      var _ref4$payload, organizationId, payload, call;

      return _regeneratorRuntime.wrap(function profileRemoveOne$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              _ref4$payload = _ref4.payload, organizationId = _ref4$payload.organizationId, payload = _ref4$payload.payload;
              call = _ref5.call;
              _context3.t0 = getResponse;
              _context3.next = 5;
              return call(profileOrgRemoveOne, organizationId, payload);

            case 5:
              _context3.t1 = _context3.sent;
              return _context3.abrupt("return", (0, _context3.t0)(_context3.t1));

            case 7:
            case "end":
              return _context3.stop();
          }
        }
      }, profileRemoveOne);
    }),
    profileSave: /*#__PURE__*/_regeneratorRuntime.mark(function profileSave(_ref6, _ref7) {
      var _ref6$payload, organizationId, payload, call;

      return _regeneratorRuntime.wrap(function profileSave$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              _ref6$payload = _ref6.payload, organizationId = _ref6$payload.organizationId, payload = _ref6$payload.payload;
              call = _ref7.call;
              _context4.t0 = getResponse;
              _context4.next = 5;
              return call(profileOrgSaveOne, organizationId, payload);

            case 5:
              _context4.t1 = _context4.sent;
              return _context4.abrupt("return", (0, _context4.t0)(_context4.t1));

            case 7:
            case "end":
              return _context4.stop();
          }
        }
      }, profileSave);
    }),
    openNewModal: /*#__PURE__*/_regeneratorRuntime.mark(function openNewModal(_, _ref8) {
      var put;
      return _regeneratorRuntime.wrap(function openNewModal$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              put = _ref8.put;
              _context5.next = 3;
              return put({
                type: 'updateState',
                payload: {
                  editModalVisible: true,
                  profileValue: [],
                  isCreate: true,
                  editModalEditable: true
                }
              });

            case 3:
            case "end":
              return _context5.stop();
          }
        }
      }, openNewModal);
    }),
    openEditModal: /*#__PURE__*/_regeneratorRuntime.mark(function openEditModal(_ref9, _ref10) {
      var _ref9$payload, organizationId, payload, call, put, profileValue;

      return _regeneratorRuntime.wrap(function openEditModal$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              _ref9$payload = _ref9.payload, organizationId = _ref9$payload.organizationId, payload = _ref9$payload.payload;
              call = _ref10.call, put = _ref10.put;
              _context6.t0 = getResponse;
              _context6.next = 5;
              return call(profileOrgQueryProfileValue, organizationId, payload);

            case 5:
              _context6.t1 = _context6.sent;
              profileValue = (0, _context6.t0)(_context6.t1);

              if (!profileValue) {
                _context6.next = 10;
                break;
              }

              _context6.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  editModalVisible: true,
                  profileValue: profileValue,
                  isCreate: false,
                  editModalEditable: true
                }
              });

            case 10:
            case "end":
              return _context6.stop();
          }
        }
      }, openEditModal);
    }),
    openViewModal: /*#__PURE__*/_regeneratorRuntime.mark(function openViewModal(_ref11, _ref12) {
      var _ref11$payload, organizationId, payload, call, put, profileValue;

      return _regeneratorRuntime.wrap(function openViewModal$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              _ref11$payload = _ref11.payload, organizationId = _ref11$payload.organizationId, payload = _ref11$payload.payload;
              call = _ref12.call, put = _ref12.put;
              _context7.t0 = getResponse;
              _context7.next = 5;
              return call(profileOrgQueryProfileValue, organizationId, payload);

            case 5:
              _context7.t1 = _context7.sent;
              profileValue = (0, _context7.t0)(_context7.t1);

              if (!profileValue) {
                _context7.next = 10;
                break;
              }

              _context7.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  editModalVisible: true,
                  profileValue: profileValue,
                  isCreate: false,
                  editModalEditable: false
                }
              });

            case 10:
            case "end":
              return _context7.stop();
          }
        }
      }, openViewModal);
    }),
    closeModal: /*#__PURE__*/_regeneratorRuntime.mark(function closeModal(_, _ref13) {
      var put;
      return _regeneratorRuntime.wrap(function closeModal$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              put = _ref13.put;
              _context8.next = 3;
              return put({
                type: 'updateState',
                payload: {
                  editModalVisible: false,
                  profileValue: [],
                  isCreate: true,
                  editModalEditable: true
                }
              });

            case 3:
            case "end":
              return _context8.stop();
          }
        }
      }, closeModal);
    }),
    profileValueRemove: /*#__PURE__*/_regeneratorRuntime.mark(function profileValueRemove(_ref14, _ref15) {
      var _ref14$payload, organizationId, payload, call;

      return _regeneratorRuntime.wrap(function profileValueRemove$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              _ref14$payload = _ref14.payload, organizationId = _ref14$payload.organizationId, payload = _ref14$payload.payload;
              call = _ref15.call;
              _context9.t0 = getResponse;
              _context9.next = 5;
              return call(profileValueOrgRemoveOne, organizationId, payload);

            case 5:
              _context9.t1 = _context9.sent;
              return _context9.abrupt("return", (0, _context9.t0)(_context9.t1));

            case 7:
            case "end":
              return _context9.stop();
          }
        }
      }, profileValueRemove);
    })
  },
  reducers: {
    // 更新状态树
    updateState: function updateState(state, _ref16) {
      var payload = _ref16.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};